package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * TODO
 *
 * @author caiyonglan
 * @email 627064518@qq.com
 * @date 2020/12/2 17:24
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

//    @Autowired
//    private MyHandlerInterceptor myHandlerInterceptor;

    /**
     * 跨域访问
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowCredentials(true)
            .allowedHeaders("*")
            .allowedOrigins("http://localhost")
            .allowedMethods("*")
            .maxAge(1600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("favicon.ico").addResourceLocations("classpath:/public/");
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        /**
//         * 添加拦截器
//         */
//        List<String> excludePath = new ArrayList<>();
//        //排除拦截，除了登录、注销，自定义拦截器不会生效，其他都拦截
//        excludePath.add("favicon.ico");
//        excludePath.add("swagger-resources");
//        excludePath.add("doc.html");
//        excludePath.add("/v2/**");
//        /*
//         *  对根目录和静态文件不需要进行拦截，如果对根目录（即登录页面）进行拦截，将会导致循环重定向
//         */
//        registry.addInterceptor(myHandlerInterceptor)
//            .addPathPatterns("/**")
//            .excludePathPatterns(excludePath);
//
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }

}
